﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class DemoEdit_Add_Delete : System.Web.UI.Page
{
    ProductDAO1 productDAO = new ProductDAO1();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // Cache sản phẩm
            if (Cache["Products"] != null)
                GridView1.DataSource = (DataTable)Cache["Products"];
            else
            {
                DataTable products = productDAO.GetAll();
                GridView1.DataSource = products;
                Cache.Insert("Products", products);
            }

            GridView1.DataBind();
            timeLabel.Text = DateTime.Now.ToString();

            // Cache thời gian hiện tại
            //if (Cache["Now"] != null)
            //    timeLabel.Text = Cache["Now"].ToString();
            //else
            //{
            //    timeLabel.Text = DateTime.Now.ToString();
            //    DateTime dateTime = DateTime.Now.AddSeconds(10);
            //    Cache.Insert("Now", DateTime.Now, null, 
            //        System.Web.Caching.Cache.NoAbsoluteExpiration,
            //        TimeSpan.FromSeconds(10));
            //}
        }
        // t5l5, t5l5

        if (editProduct != null)
            editProduct.Visible = false;
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataSource = productDAO.GetAll();
        GridView1.DataBind();

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        editProduct.Visible = !editProduct.Visible;
    }
   
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int productId = Int32.Parse(GridView1.SelectedDataKey.Value.ToString());

        editProduct.ProductId = productId;
        editProduct.Refresh();
        editProduct.Visible = true;
    }
}